package edu.wit.acl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.wit.acl.entity.Project;
import edu.wit.acl.entity.User;
import edu.wit.acl.entity.UserProject;
import edu.wit.common.annotation.Log;
import edu.wit.common.enums.BusinessType;
import edu.wit.common.result.R;
import edu.wit.common.security.service.TokenService;
import edu.wit.common.utils.ServletUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
//import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin/acl/project")
@Api(tags = "项目模块")
public class ProjectController extends BaseController {

    @Autowired
    private TokenService tokenService;

    @ApiOperation(value = "根据用户获取项目数据")
    @GetMapping("/getProjectList")
//    @PreAuthorize("hasAuthority('sys:menu:list')")
//    @PreAuthorize("hasRole('---')")
    @Log(title = "根据用户获取项目数据", businessType = BusinessType.SELECT)
    public R getProjectList() {
        String token = ServletUtils.getRequest().getHeader("token");
        User user = userService.selectByUsername(tokenService.getLoginUser(token).getUsername());
        String flag = "0";
        String roleId = userRoleService.selectRoleIdByUserId(user.getId());
        /*
           通过判断user的pid来判断用户是否为超管级别---------------------------租户账号中仅有超管级别无pid属性
           如果是超管管理员进入，就按照超管用户获取项目列表
           如果是非超管 管理员进入或者劳务公司人员进入就按照 相对应你角色获取项目列表
         */
        List<Project> projectList = new ArrayList<>();

        if (!user.getPid().equals("0")) {
            List<String> projectIds = roleProjectService.selectProjectIdsByRoleId(roleId + "");
            projectList = projectService.selectProjectByProjectIds(projectIds);
            flag = "1";
        } else {
            projectList = projectService.selectProjectByUserId(user.getId());
        }

        return R.ok().data("projectList", projectList).data("flag", flag);
    }

    @ApiOperation(value = "添加项目")
    @PostMapping("/projectSave")
    @Log(title = "添加项目", businessType = BusinessType.INSERT)
    public R save(@RequestBody Project project) throws Exception {
        projectService.save(project);
        Project tempProject = projectService.selectProjectByProjectName(project.getName());
        User tempUser = userService.selectByUsername(tokenService.getLoginUserByToken().getUsername());
        if (!tempUser.getPid().equals("0")) {
            userProjectService.save(new UserProject().setUserId(tempUser.getPid()).setProjectId(tempProject.getId()));
        } else {
            userProjectService.save(new UserProject().setUserId(tempUser.getId()).setProjectId(tempProject.getId()));
        }
        return R.ok();
    }

    @ApiOperation(value = "修改项目")
    @PostMapping("/projectUpdate")
    public R update(@RequestBody Project project) throws Exception {
        projectService.updateById(project);
        return R.ok();
    }

    @ApiOperation(value = "删除项目")
    @PostMapping("/projectRemove/{id}")
    public R save(@PathVariable String id) {
        projectService.deleteProjectById(id);
        userProjectService.remove(new QueryWrapper<UserProject>().eq("project_id", id));
        return R.ok();
    }

    @ApiOperation(value = "根据项目id获取项目信息")
    @PostMapping("/selectProject/{id}")
    public R selectProject(@PathVariable String id) {
        return R.ok().data("project", projectService.selectProjectByProjectId(id));
    }

    @ApiOperation(value = "通过角色获取项目")
    @GetMapping("/getProjectIdsByRole/{roleId}")
    public R getProjectIdsByRole(@PathVariable String roleId)  {
        return R.ok().data("myProjectIds",roleProjectService.selectProjectIdsByRoleId(roleId));
    }
}
